package com.example.component;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CheckBox;
import android.widget.Checkable;
import android.widget.LinearLayout;

public class CheckableLinearLayout extends LinearLayout implements Checkable {
	private CheckBox mCheckBox;

	public CheckableLinearLayout(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	public CheckableLinearLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public CheckableLinearLayout(Context context) {
		super(context);
	}

	@Override
	protected void onFinishInflate() {
		super.onFinishInflate();
		mCheckBox = (CheckBox) findViewById(android.R.id.checkbox);
		if (mCheckBox == null) {
			throw new RuntimeException(
					"Your content must have a CheckBox whose id attribute is " +
							"'android.R.id.checkbox'");
		}

		mCheckBox.setClickable(false);
		mCheckBox.setFocusable(false);
		mCheckBox.setFocusableInTouchMode(false);

	}

	public void showCheckBox(boolean checkable) {
		if (checkable) {
			mCheckBox.setVisibility(View.VISIBLE);
		} else {
			mCheckBox.setVisibility(View.GONE);
		}
	}

	@Override
	public void setChecked(boolean checked) {
		mCheckBox.setChecked(checked);
	}

	@Override
	public boolean isChecked() {
		return mCheckBox.isChecked();
	}

	@Override
	public void toggle() {
		mCheckBox.toggle();
	}
}
